import 'dart:async';
import 'dart:io';

void disposable() {
  print('start');
  Timer(Duration(milliseconds: 200), () => {print("这是一次性定时器")});
  print('end');
}

void periodic() async{
  int i = 1;
  print('start');
  var timer = Timer.periodic(Duration(seconds: 1), (timer) {
    print(i++);
    if (i > 10) {
      timer.cancel();
    }
  });
  print('end');

  // sleep(const Duration(seconds: 3));
  await Future.delayed(const Duration(seconds: 3));
  timer.cancel();
}

microtask() {
  var futureInstance = Future.value(1234);

  for (var i = 0; i < 1; i++) {
    Future.microtask(() => print("这是Future.microtask()方法"));
    externalApproach();
    print('第一个for循环: $i');
    futureInstance.then((val) => print(val));
    Timer.run(methods);
  }
  for (var i = 0; i < 2; i++) {
    print('第二个for循环: $i');
  }
  Timer(Duration(milliseconds: 1), () => {print("一次性的定时器")});
}

void methods() async {
  print("尽快异步运行给定的回调");
}

void externalApproach() {
  print("这是外部方法");
}

main(List<String> args) {
  // disposable();
  periodic();
  // microtask();
}
